package gob.inei.enaho.model.service;

import gob.inei.enaho.model.domain.persona.Personal;

import java.sql.SQLException;
import util.android.sqlite.CommandSQLite;
import util.android.sqlite.SQLiteUtil;
import android.database.Cursor;


public class PersonalService 
extends AbstractService
{
	
	@Override
	protected String getNombreTabla() {
		return "Personal";
	}
		
	public void setear(Cursor cr, Personal obj) {
		obj.setDni(SQLiteUtil.getString(cr, "DNI"));
        
        obj.setNombres(SQLiteUtil.getString(cr, "NOMBRE"));
       
        obj.setApellidoPaterno(SQLiteUtil.getString(cr, "APEPATERNO"));
        
        obj.setApellidoMaterno(SQLiteUtil.getString(cr, "APEMATERNO"));               
        
        //obj.setCargo(cargo)
	}
	
		
	public Personal findSupervisorLocalActivoByDni(String dni) throws SQLException {
		return findPersonalActivoByDniCargo(dni,"03");
	}	

	public Personal findSupervisorNacionalActivoByDni(String dni) throws SQLException {
		return findPersonalActivoByDniCargo(dni,"01");
	}

	public Personal findCoordinadorDepartamentalActivoByDni(String dni) throws SQLException {
		return findPersonalActivoByDniCargo(dni,"02");
	}

	private Personal findPersonalActivoByDniCargo(String dni, String cargo) throws SQLException {
		String sql = "select * from Personal where dni='"+dni+"' and cargo='"+cargo+"'";
		return (Personal) SQLiteUtil.executeQueryUnaVez(getOpenHelper(), sql, new CommandSQLite() {			
			@Override
			public Object execute(Cursor cr) throws SQLException {
				Personal p = new Personal();
				setear(cr, p);
				return p;
			}
		});
	}
	
	
}
